package com.sias.oj.wxmp;

import com.sias.oj.wxmp.handler.EventHandler;
import com.sias.oj.wxmp.handler.MessageHandler;
import com.sias.oj.wxmp.handler.SubscribeHandler;
import me.chanjar.weixin.common.api.WxConsts.EventType;
import me.chanjar.weixin.common.api.WxConsts.XmlMsgType;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * 微信公众号路由
 *
 * @author <a href="https://github.com/sogra">程序员sogra</a>
 */
@Configuration
public class WxMpMsgRouter {

  @Resource
  private WxMpService wxMpService;

  @Resource
  private EventHandler eventHandler;

  @Resource
  private MessageHandler messageHandler;

  @Resource
  private SubscribeHandler subscribeHandler;

  @Bean
  public WxMpMessageRouter getWxMsgRouter() {
    WxMpMessageRouter router = new WxMpMessageRouter(wxMpService);
    // 消息
    router.rule()
            .async(false)
            .msgType(XmlMsgType.TEXT)
            .handler(messageHandler)
            .end();
    // 关注
    router.rule()
            .async(false)
            .msgType(XmlMsgType.EVENT)
            .event(EventType.SUBSCRIBE)
            .handler(subscribeHandler)
            .end();
    // 点击按钮
    router.rule()
            .async(false)
            .msgType(XmlMsgType.EVENT)
            .event(EventType.CLICK)
            .eventKey(WxMpConstant.CLICK_MENU_KEY)
            .handler(eventHandler)
            .end();
    return router;
  }
}
